#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<vector<bool>> check(m, vector<bool>(n, true));

        int dx[4] = { 0,0,1,-1 };
        int dy[4] = { 1,-1,0,0 };
        function<void(int, int)> dfs = [&](int r, int c)
            {
                check[r][c] = false;
                for (int i = 0; i < 4; i++)
                {
                    int x = r + dx[i];
                    int y = c + dy[i];
                    if (x >= 0 && x < m && y >= 0 && y < n && check[x][y] && grid[x][y] == '1')
                        dfs(x, y);
                }
            };

        int ret = 0;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (check[i][j] && grid[i][j] == '1')
                    dfs(i, j), ret++;
        return ret;
    }
};